package controllers

import (
	"encoding/json"
	_ "fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	_ "github.com/goinggo/mapstructure"
	"strconv"
	"tmp_report/models"
)

/*
@Project        :   tmp_report
@FileName       :   staffcontrollers.go
@Author         :   tangchaolizi
@Datetime       :   2020/7/26 5:59 下午
@Software       :   GoLand
@Show           :
*/

type StaffController struct {
	beego.Controller
}

// 人员信息主页展示
func (c *StaffController) Get() {
	username := c.GetSession("Username")
	c.Data["IsLogin"] = username
	c.TplName = "tmp/search_history.html"
}

// 获取人员信息，Json
func (c *StaffController) GetStaffs() {
	var Staffs StaffJson
	o := orm.NewOrm()
	qs := o.QueryTable("Staff")
	_, _ = qs.All(&Staffs.Data)
	Staffs.Status = true
	Staffs.Code = 0
	c.Data["json"] = Staffs // Json输出
	c.ServeJSON()
}

// 添加人员
func (c *StaffController) Create() {
	var Staff models.Staff
	var Jsons Jsons
	name := c.GetString("name")
	Staff.Name = name
	o := orm.NewOrm()
	_, err := o.Insert(&Staff)
	if err != nil {
		beego.Warning(err)
	}
	Jsons.Status, Jsons.Msg = 1, "添加成功" // Json输出
	c.Data["json"] = Jsons
	c.ServeJSON()
}

// 删除人员
func (c *StaffController) Delete() {
	var Jsons Jsons
	idString := c.GetString("id")
	var id int
	id, _ = strconv.Atoi(idString) // String2Int
	o := orm.NewOrm()
	_, err := o.Delete(&models.Staff{Id: id}) // 数据库删除该ID人员xinxi
	if err != nil {
		beego.Warning(err)
		return
	}
	Jsons.Status, Jsons.Msg = 1, "删除成功" // Json输出
	beego.Info(Jsons)
	data, _ := json.MarshalIndent(Jsons, "", "")
	c.Ctx.WriteString(string(data))
}
